<?php
/**
 * Created By PhpStorm
 * Author: patrick wu
 * Date: 2020/12/21
 * Time: 14:48
 */
namespace App\Service\Api;

use App\Http\Model\Mysql\UserModel;
use App\Utils\SendSms;

class PromoterService extends BaseService
{
    /**
     * 发送验证码
     * @param string $phone
     * @return array
     * @throws \AlibabaCloud\Client\Exception\ClientException
     */
    public function promoterCode(string $phone)
    {
        $user_info  =   request()->post('api_user');
        if (!UserModel::where(['uid' => $user_info['user_id'], 'level' => 1])->exists()) {
            return [false, '非法请求'];
        }
        return SendSms::sendCode($user_info['user_id'], $phone);
    }

    /**
     * 申请成为推广员
     * @param array $params
     * @return array
     */
    public function promoterConfirm(array $params)
    {
        $user_info  =   request()->post('api_user');
        if (!SendSms::checkCode($user_info['user_id'], $params['phone'], $params['check_code'])) {
            return [false, '验证码错误'];
        }

        $result =   UserModel::where(['uid' => $user_info['user_id']])->update([
            'level'     =>  2,
            'relname'   =>  $params['real_name'] ?? '',
            'wx_number' =>  $params['wechat'] ?? ''
        ]);

        if ($result) {
            return [true, '请求成功'];
        } else {
            return [false, '请求失败'];
        }
    }
}
